﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Prism.Games.GalEngine.Scripts.Data.ResourceCollection;

namespace Prism.Games.GalEngine.Scripts.Parser
{
    public class MemoryDataStore
    {
        public MemoryDataStore()
        {
            this.Roles = new RoleCollection();
        }

        protected RoleCollection Roles { get; set; }

        public virtual void OnStoreRoles(RoleCollection roles)
        {
            this.Roles.Clear();
            foreach (var i in roles)
            {
                Roles.Add(new Data.Role() { Name = i.Name, Id = i.Id, Cohesion = i.Cohesion });
            }
        }

        public virtual void OnRoleReset(RoleCollection roles)
        {
            foreach (var i in roles)
            {
                var q = from j in this.Roles
                        where j.Id == i.Id
                        select j;
                if (q.Count() == 1)
                {
                    var target = q.Single();
                    i.Name = target.Name;
                    i.Cohesion = target.Cohesion;
                }
            }
        }

        public virtual void OnStoreRoles(RoleCollection inRoles, RoleCollection outRoles)
        {
            outRoles.Clear();
            foreach (var i in inRoles)
            {
                outRoles.Add(new Data.Role() { Name = i.Name, Id = i.Id, Cohesion = i.Cohesion });
            }
        }

        /// <summary>
        /// 将In设置为Out。
        /// </summary>
        /// <param name="inRoles"></param>
        /// <param name="outRoles"></param>
        public virtual void OnRoleReset(RoleCollection inRoles, RoleCollection outRoles)
        {
            foreach (var i in inRoles)
            {
                var q = from j in outRoles
                        where j.Id == i.Id
                        select j;
                if (q.Count() == 1)
                {
                    var target = q.Single();
                    i.Name = target.Name;
                    i.Cohesion = target.Cohesion;
                }
            }
        }
    }


}
